<?php
global $hide_right_sidebar, $nyhet_side, $valgt_nyhet, $wpdb;
if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action'])) {
    switch ($_POST['action']) {
        case 'Oppdater': // Kjører hvis knappen "Oppdater" trykkes i rediger-siden

//            $post_to_edit = array();
            $valgt_nyhet = $_POST['postid'];
            $post_to_edit = get_post($valgt_nyhet);

            /* these are the fields that we are editing in the form below. you have to change them to your fields and you can add as many as you need. */

            $title = $_POST['title'];
            $description = $_POST['description'];

            /* this code will save the title and description into the post_to_edit array */

            $post_to_edit->post_title = $title;
            $post_to_edit->post_content = $description;


            /* honestly i can't really remember why i added this code but it is a must */

            $pid = wp_update_post($post_to_edit);

            /* save taxonomies: post ID, form name, taxonomy name, if it appends(true) or rewrite(false) */

            wp_set_post_terms($pid, array($_POST['cat']), 'categories', false);
            wp_set_post_terms($pid, array($_POST['post_tags']), 'tags', false);
            /* Redirect so the page will show updated info. */
            /* I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
            if (count($error) == 0) {
                //action hook for plugins and extra fields saving
//                $wpdb->update('arrangement', $oppdater, array('arrangementid' => $valgt_aktivitet));
                $nyhet_side = 'vis';
//                $valgt_nyhet = $pid;
//                wp_redirect(get_permalink($valgt_nyhet));
//                exit;
            } else {
                echo 'dette gikk ikke som forventet.';
            }
            break;
        case 'Rediger': // Kjører hvis knappen "Rediger" trykkes i vis-siden
            $valgt_nyhet = $_POST['nyhetID'];
            $nyhet_side = 'rediger';
            break;
        case 'Avbryt': // Kjører hvis knappen "Avbryt" trykkes i rediger-siden
        case 'Vis nyhet': // Kjører hvis knappen "Oppdater" trykkes i rediger-siden
//            $valgt_nyhet = $_POST['id-value'];
//            $nyhet_side = 'vis';
            wp_redirect(get_permalink($valgt_nyhet));
            exit();
            break;
        case 'Ny Nyhet':
            $valgt_nyhet = 0;
            $nyhet_side = 'leggtil';
            break;
        case 'Legg Til':
            if (!isset($_POST['title']) || !isset($_POST['description'])) {
                if (!isset($_POST['title']))
                    echo 'Du glemte nyhetens tittel';

                if (!isset($_POST['description']))
                    echo 'Du glemte å skrive noe om den';

                echo '';
            } else {
                $title = $_POST['title'];
                $description = $_POST['description'];
            }


            $tags = $_POST['post_tags'];

            // ADD THE FORM INPUT TO $new_post ARRAY
            $new_post = array(
                'post_title' => $title,
                'post_content' => $description,
                'post_category' => array($_POST['cat']), // Usable for custom taxonomies too
                'tags_input' => array($tags),
                'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
                'post_type' => 'post'  //'post',page' or use a custom post type if you want to
            );

            //SAVE THE POST
            $pid = wp_insert_post($new_post);

            //SET OUR TAGS UP PROPERLY
            wp_set_post_tags($pid, $_POST['post_tags']);

            /* Redirect so the page will show updated info. */
            /* I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
            if (count($error) == 0) {
                //action hook for plugins and extra fields saving
                do_action('wp_insert_post', 'wp_insert_post');

                $valgt_nyhet = $pid;
//                $nyhet_side = 'vis';
                echo $valgt_nyhet;
                wp_redirect(get_permalink($valgt_nyhet));
            exit();
            }      
            break;
        default: // Kjører hvis en ukjent knapp kjøres
            echo "Knappen '" . $_POST['action'] . "' har ikke en definert funksjon";
            break;
    }
}
if ($nyhet_side == 'vis') {
    header('Location: ' . get_permalink($valgt_nyhet));
    exit;
} else {
?>
<div id="ham-aktivitet">
    <?php
//    if (current_user_can('vis_aktivitet'))
//        ;
    include('nyhet-' . $nyhet_side . '.php');
    ?>
</div>
<?php } ?>